package com.lostreseditores.sabersai.dataaccess.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

import com.lostreseditores.sabersai.dataaccess.sessionFactory.HibernateSessionFactory;
import com.lostreseditores.sabersai.model.Bases;


public class BasesDAO implements IBasesDAO {
    private static final Log log = LogFactory.getLog(BasesDAO.class);

    // property constants
    //public static final Integer  BASEIDN = "baseidn";
    public static final String BASEIDN = "baseidn";

    //public static final Long  BCOMPETENCIAFINN = "bcompetenciafinn";
    public static final String BCOMPETENCIAFINN = "bcompetenciafinn";

    //public static final Long  BCOMPETENCIAININ = "bcompetenciainin";
    public static final String BCOMPETENCIAININ = "bcompetenciainin";

    //public static final Long  BCOMPONENTEFINN = "bcomponentefinn";
    public static final String BCOMPONENTEFINN = "bcomponentefinn";

    //public static final Long  BCOMPONENTEININ = "bcomponenteinin";
    public static final String BCOMPONENTEININ = "bcomponenteinin";

    //public static final String  BDESCRIPCIONV = "bdescripcionv";
    public static final String BDESCRIPCIONV = "bdescripcionv";

    //public static final Long  BDIFERENCIAN = "bdiferencian";
    public static final String BDIFERENCIAN = "bdiferencian";

    //public static final Long  BPROMEDIOFINN = "bpromediofinn";
    public static final String BPROMEDIOFINN = "bpromediofinn";

    //public static final Long  BPROMEDIOININ = "bpromedioinin";
    public static final String BPROMEDIOININ = "bpromedioinin";

    //public static final Long  BTOTALFINN = "btotalfinn";
    public static final String BTOTALFINN = "btotalfinn";

    //public static final Long  BTOTALININ = "btotalinin";
    public static final String BTOTALININ = "btotalinin";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Bases Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Bases instance) {
        log.debug("saving Bases instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Bases Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Bases instance) {
        log.debug("deleting Bases instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Bases Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Bases instance) {
        log.debug("updating Bases instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Bases findById(Integer id) {
        log.debug("finding Bases instance with id: " + id);

        try {
            Bases instance = (Bases) getSession().get(Bases.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Bases failed", re);
            throw re;
        }
    }

    public List<Bases> findByExample(Bases instance) {
        log.debug("finding Bases instance by example");

        try {
            List results = getSession().createCriteria("Bases")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Bases entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Bases> found by query
        */
    public List<Bases> findByProperty(String propertyName, Object value) {
        log.debug("finding Bases instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Bases as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Bases> findByBaseidn(Object baseidn) {
        return findByProperty(BASEIDN, baseidn);
    }

    public List<Bases> findByBcompetenciafinn(Object bcompetenciafinn) {
        return findByProperty(BCOMPETENCIAFINN, bcompetenciafinn);
    }

    public List<Bases> findByBcompetenciainin(Object bcompetenciainin) {
        return findByProperty(BCOMPETENCIAININ, bcompetenciainin);
    }

    public List<Bases> findByBcomponentefinn(Object bcomponentefinn) {
        return findByProperty(BCOMPONENTEFINN, bcomponentefinn);
    }

    public List<Bases> findByBcomponenteinin(Object bcomponenteinin) {
        return findByProperty(BCOMPONENTEININ, bcomponenteinin);
    }

    public List<Bases> findByBdescripcionv(Object bdescripcionv) {
        return findByProperty(BDESCRIPCIONV, bdescripcionv);
    }

    public List<Bases> findByBdiferencian(Object bdiferencian) {
        return findByProperty(BDIFERENCIAN, bdiferencian);
    }

    public List<Bases> findByBpromediofinn(Object bpromediofinn) {
        return findByProperty(BPROMEDIOFINN, bpromediofinn);
    }

    public List<Bases> findByBpromedioinin(Object bpromedioinin) {
        return findByProperty(BPROMEDIOININ, bpromedioinin);
    }

    public List<Bases> findByBtotalfinn(Object btotalfinn) {
        return findByProperty(BTOTALFINN, btotalfinn);
    }

    public List<Bases> findByBtotalinin(Object btotalinin) {
        return findByProperty(BTOTALININ, btotalinin);
    }

    /**
    * Find all Bases entities.
    *
    * @return List<Bases> all Bases instances
    */
    public List<Bases> findAll() {
        log.debug("finding all Bases instances");

        try {
            String queryString = "from Bases";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Bases> findByCriteria(String whereCondition) {
        log.debug("finding Bases " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Bases model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Bases> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Bases failed", re);
            throw re;
        }
    }

    public List<Bases> findPageBases(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Bases findPageBases");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Bases model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Bases model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberBases() {
        log.debug("finding Bases count");

        try {
            String queryString = "select count(*) from Bases model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
